package com.kanqiubao.kq.fragment;

import android.view.View;

import com.kanqiubao.kq.R;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.List;

import okhttp3.Call;

public abstract class RecyclerViewBaseRefreshFragment<T> extends BaseRefreshFragment<T> implements SuperRecyclerView.LoadingListener,
        SuperBaseAdapter.OnItemClickListener {
    protected SuperRecyclerView mRecyclerView;
    protected SuperBaseAdapter<T> mSuperBaseAdapter;
    protected boolean isFirst;
    protected int mType;

    public void setmType(int type) {
        mType = type;
    }

    public void setFirst(boolean isFirst) {
        this.isFirst = isFirst;
    }


    @Override
    protected void initView() {
        mRecyclerView = findView(R.id.view_holder);
        if (mSuperBaseAdapter != null && mRecyclerView != null) {
            mRecyclerView.setAdapter(mSuperBaseAdapter);
            mSuperBaseAdapter.setOnItemClickListener(this);

        }
        if (mRecyclerView != null) {
            mRecyclerView.setRefreshEnabled(true);// 可以定制是否开启下拉刷新
            mRecyclerView.setLoadMoreEnabled(true);// 可以定制是否开启加载更多
            mRecyclerView.setLoadingListener(this);// 下拉刷新，上拉加载的监听
            mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);// 下拉刷新的样式
            mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallPulse);// 上拉加载的样式
            mRecyclerView.setArrowImageView(R.mipmap.ic_pulltorefresh_arrow);// 设置下拉箭头
        }

    }

    @Override
    public void onRefresh() {
        isRefresh = true;
        pageIndex = 1;
        requestData();

    }

    @Override
    public void onLoadMore() {
        isRefresh = false;
        pageIndex++;
        requestData();

    }

    @Override
    public void onSuccess(List<T> paramT, Call paramCall, okhttp3.Response paramResponse) {
        successData(paramT);
        super.onSuccess(paramT, paramCall, paramResponse);
    }

    @Override
    public void onCacheSuccess(List<T> ts, Call call) {
        successData(ts);
        super.onCacheSuccess(ts, call);
    }

    @Override
    public void onError(Call call, okhttp3.Response response, Exception e) {
        error();
        super.onError(call, response, e);
    }

    protected void successData(List<T> data) {
        isFirst = true;
        if (mRecyclerView == null) {
            return;
        }
        mRecyclerView.completeLoadMore();
        mRecyclerView.completeRefresh();
        if (isRefresh) {
            if (data != null && data.size() > 0) {
                isVisableView(0);
            } else {
                isVisableView(1);
            }
        }
        if (data != null) {
            List ls = (List) data;
            if (ls.size() > 0) {
                if (isRefresh) {
                    mSuperBaseAdapter.clearAll();
                }
                isRefresh = true;
                if (ls.size() < pageSize) {
                    mRecyclerView.setNoMore(true);
                    if (mSuperBaseAdapter.getItemCount() < 10) {
                        mRecyclerView.setLoadMoreEnabled(false);
                    }
                } else {
                    mRecyclerView.setNoMore(false);
                    mRecyclerView.setLoadMoreEnabled(true);
                }
                mSuperBaseAdapter.addData(ls);
            } else {
                mRecyclerView.setNoMore(true);
                if (mSuperBaseAdapter.getItemCount() < 10) {
                    mRecyclerView.setLoadMoreEnabled(false);
                }
            }
        }
    }

    protected void error() {
        if (isRefresh && mSuperBaseAdapter.getItemCount() == 0) {
            // 网络异常
            isVisableView(2);
        }
        pageIndex = pageIndex == 1 ? 1 : --pageIndex;
        if (mRecyclerView != null) {
            mRecyclerView.completeLoadMore();
            mRecyclerView.completeRefresh();
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setmSearchKey(String mSearchKey) {
        super.setmSearchKey(mSearchKey);
        isFirstLoad = true;
        isRefresh = true;
        pageIndex = 1;
    }

    @Override
    public void initRequest() {
        emptyLayoutClick();
    }

}
